package com.putao;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import com.putao.entity.ZhihuData;

public class Server {
	private final static Logger LOGGER = LoggerFactory.getLogger(Server.class);
	ApplicationContext applicationContext;
	JdbcTemplate jdbcTemplate;

	public Server() {
		applicationContext = new ClassPathXmlApplicationContext("spring-jdbc.xml");
		jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
	}

	public void addTopic(String catId, String catName, String topicId, String topicName) {
		LOGGER.info("---begin add---catId:{},catName:{},topicId:{},topicName:{}", catId, catName, topicId, topicName);
		try {
			jdbcTemplate.update("insert into t_zhihu_topic(cat_id,cat_name,topic_id,topic_name) values(?,?,?,?)",
					new Object[] { catId, catName, topicId, topicName });
		} catch (Exception e) {
			LOGGER.error("--add error--", e);
		}
	}

	public void batchAddTopic(final List<ZhihuData> datas) {
		LOGGER.info("---begin add---params:{}", datas);
		for (ZhihuData zhihuData : datas) {
			this.addTopic(zhihuData.getCatId(), zhihuData.getCatName(), zhihuData.getTopicId(),
					zhihuData.getTopicName());
		}
		// jdbcTemplate.batchUpdate("insert into
		// t_zhihu_topic(cat_id,cat_name,topic_id,topic_name) values(?,?,?,?)",
		// new BatchPreparedStatementSetter() {
		//
		// @Override
		// public void setValues(PreparedStatement ps, int i) throws
		// SQLException {
		// ps.setString(1, datas.get(i).getCatId());
		// ps.setString(2, datas.get(i).getCatName());
		// ps.setString(3, datas.get(i).getTopicId());
		// ps.setString(4, datas.get(i).getTopicName());
		// }
		//
		// @Override
		// public int getBatchSize() {
		// return datas.size();
		// }
		// });
	}
}
